package fun.xinnix.concurrent;

import cn.hutool.log.StaticLog;

import java.util.concurrent.*;

public class ThreadLocalDemo {
    private static final ThreadLocal<Integer> num = ThreadLocal.withInitial(() -> 0);
    private static final ThreadLocal<Integer> count = ThreadLocal.withInitial(() -> 20);
    private static final ThreadPoolExecutor threadPool = ThreadPoolDemo.getThreadPool();

    public static void main(String[] args) {
        try {
            for (int i = 0; i < 20; i++) {
                int index = i;
                threadPool.execute(() -> {
                    for (int j = 0; j <= 20; j++) {
                        StaticLog.info("id:{},num:{},count:{}", index, num.get(), count.get());
                        num.set(num.get() + 1);
                        count.set(count.get() - 1);
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
            threadPool.shutdown();
        }
    }
}
